package base.thread_JUC2.chapter03.demo04;

import java.util.concurrent.CyclicBarrier;

public class TestCase {

    public static void main(String[] args) {
        final int ROWS=10000;
        final int NUMBERS=1000;
        final int SEARCH= 6;
        final int PARTICIPANTS=5;
        final int LINES_PARTICIPANT=2000;

        MatrixMock matrixMock = new MatrixMock(ROWS, NUMBERS, SEARCH);
        Results results = new Results(ROWS);
        GrouperRunnable grouperRunnable = new GrouperRunnable(results);
        //PARTICIPANTS： 在阻塞被唤醒之前必须调用 await 的线程数
        //grouperRunnable：最终CyclicBarrier要执行的任务
        CyclicBarrier cyclicBarrier = new CyclicBarrier(PARTICIPANTS, grouperRunnable);
        for (int i = 0; i < PARTICIPANTS; i++) {
            SearcherRunnable searcherRunnable =
                new SearcherRunnable(i * LINES_PARTICIPANT, (i * LINES_PARTICIPANT) + LINES_PARTICIPANT, matrixMock, results, SEARCH, cyclicBarrier);

            Thread thread = new Thread(searcherRunnable);
            thread.start();
        }
        System.out.printf("Main: The main thread has finished.\n");
    }


}
